<?php

namespace App\Models;

class Vm extends Base
{

    protected $table = 'la_vms';

    protected $guarded = [];

    // 类型 1-'阿里云',2-'腾讯云'
    const TYPE_ALIYUN = 1;
    const TYPE_TENCENT = 2;

    const TYPE_MAP = [
        self::TYPE_ALIYUN => '阿里云',
        self::TYPE_TENCENT => '腾讯云',
    ];

    /**
     * 格式化列表数据
     */
    public static function formatDatas($list, $params = '')
    {

        foreach ($list as &$item) {
            $item = self::formatItem($item, $params);
        }

        return $list;
    }

    /**
     * 格式化单个数据
     */
    public static function formatItem($item, $params = '')
    {
        if (!$item) {
            return '';
        }


        $item['type_name'] = self::TYPE_MAP[$item['type']] ?? '-';
        $item['user_id'] = VmAdmin::query()->where('vm_id', $item['id'])->pluck('admin_id');
        if ($item['user_id']){
            $item['nickname'] = VueAdmin::query()->whereIn('id', $item['user_id'])->pluck('nickname');
        }
        return $item;
    }
}
